package com.cosina.game.crystallight.ui.mapselect;

import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

import com.cosina.game.crystallight.CrystalLightActivity;
import com.cosina.game.crystallight.util.DialogUtil;

public class UpAndDownLayout extends LinearLayout {

	
	private DownPart downPart;
	private MapView mapView;
	
	private final CrystalLightActivity activity;
	public UpAndDownLayout(CrystalLightActivity activity) {
		super(activity);
		this.activity = activity;
		this.setOrientation(LinearLayout.VERTICAL);
		this.downPart = new DownPart(activity,this);
		this.mapView = new MapView(activity,this);
		this.setView(mapView, downPart);
		mapView.scrollTo(0, 1506);
	}
	
	private View up, down;
	
	
	private void setView(View up, View down){
		this.removeView(this.up);
		this.removeView(this.down);
		
		this.up = up;
		this.down = down;
		
		this.addView(up);
		this.addView(down);
	}

	public void showMap(){
		this.setView(mapView, downPart);
		this.invalidate();
	}
	
	public void doShowTollGate(int tollGateId) {
		TollGateSelectView tollGateSelectView = new TollGateSelectView(this.getContext(), this);
		tollGateSelectView.tollGateId = tollGateId;
		this.setView(tollGateSelectView, downPart);
		this.invalidate();
	}
	
	public void showStats(){
		this.setView(new StatsUpPart(this.getContext()), downPart);
		this.invalidate();
	}
	
	public void showSkills(){
		this.setView(new SkillPart(this.getContext()), downPart);
		this.invalidate();
	}
	
	public void showHighScore(){
		this.setView(new ScorePart(this.getContext()), downPart);
		this.invalidate();
	}
	public final static int UP_HEIGHT = 480 - 66;
	public final static int DOWN_HEIGHT =  66;
	
	public boolean onTouchEvent(MotionEvent event) {
		if(event.getY() >= UP_HEIGHT){
			downPart.dispatchTouchEvent(event);
		}else{
			up.dispatchTouchEvent(event);
		}
		return true;
	}

	public void playTollGate(int tollGateId) {
		Log.e("cosina1985", "play tollgate " + tollGateId);
		activity.doPlayTollGate(tollGateId);
	}
	
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK){
			doShowQuiteDialog();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	

	private void doShowQuiteDialog() {
		DialogUtil.show(this.getContext(), "Crystallight", "Exit to Menu?", "YES", "NO", goBackRunnable);
	}
	
	
	private final Runnable goBackRunnable = new Runnable(){
		@Override
		public void run() {
			((CrystalLightActivity)(UpAndDownLayout.this.getContext())).doShowMenu();
		}
	};
}
